/**
 * 首页管理
 * @Author: Damen
 * @Data: Created in 2018/3/18
 */
var urlParams = {
    loginAction: webRoot + "/login/",
    loginoutAction: webRoot + "/user/user/loginout",
    contAction: webRoot + "/purchaseRecord/user/selectPageInfo",
    revenueAction: webRoot + "/revenueRecord/user/selectRecordPageInfo",
    rechargeAction: webRoot + "/rechargeRecord/user/selectPageInfo",
    presentAction: webRoot + "/presentRecord//user/selectPageInfo"
};
var contGrid, contStore;             //合约
var revenueGrid, revenueStore;       //收益
var rechargeGrid, rechargeStore;     //充值
var presentGrid, presentStore;       //提现
var userInfo = session.userInfo;
var gridTab;

$(function () {
    Bui.tab({
        srcNode: '#tab',
        elCls: 'nav-tabs',
        itemStatusCls: {
            'selected': 'active'
        },
        panelContainer: '#panel'//如果不指定容器的父元素，会自动生成
    }, function (tab) {
        gridTab = tab;
    });
    initData();
    initContGrid();
    initRevenueGrid();
    initRechargeGrid();
    initPresentGrid();

    initEvent();
});

function initData() {
    if (userInfo) {
        $("#userId").text(userInfo.id);
        $("#userName").text(userInfo.userName);
        $("#phoneNumber").text(userInfo.phoneNumber);
    }
    CommonClass.BaseAction({
        sqlKey: 'com.mg.mapper.PowerAccountMapper.selectAccountByUserId',
        paramMap: {userId: userInfo.id}
    }, function (data) {
        var powerText = "0/出售中：0";
        var balance = 0;
        var frozenComputingPower = 0;
        if (data && data[0]) {
            powerText = data[0].computingPower - data[0].frozenComputingPower + "/出售中：" + data[0].frozenComputingPower;
            balance = data[0].balance;
            frozenComputingPower = data[0].frozenComputingPower;
        }
        $(".computingPower").text(powerText);
        // $(".balance").text(balance);
        $(".frozenComputingPower").text(frozenComputingPower);
    });
    var day1 = new Date();
    day1.setTime(day1.getTime() - 24 * 60 * 60 * 1000);
    var dayNum = day1.getDate();
    if (dayNum < 10) {
        dayNum = "0" + dayNum;
    }
    var month = (day1.getMonth() + 1);
    if (month < 10) {
        month = "0" + month;
    }
    var s1 = day1.getFullYear() + "-" + month + "-" + dayNum;
    CommonClass.BaseAction({
        sqlKey: 'com.mg.mapper.RevenueRecordMapper.selectByUserAndTime',
        paramMap: {userId: userInfo.id, revernueTime: s1}
    }, function (data) {
        var profit = "0 元";
        var ethProfit = "--";
        if (data && data[0]) {
            for (var i = 0; i < data.length; i++) {
                if (data[i].revenueType == "M") {
                    profit = data[i].profit || 0 + " 元";

                }
                if (data[i].revenueType == "E") {
                    ethProfit = data[i].profit || "--";
                }
            }
        }
        $(".amount").text(profit);
        $(".ethAmount").text(ethProfit);
    });
    CommonClass.BaseAction({
        sqlKey: 'com.mg.mapper.RevenueRecordMapper.selectByUserAndTime',
        paramMap: {userId: userInfo.id}
    }, function (data) {
        var profit = "0 元";
        var ethProfit = "--";
        if (data && data[0]) {
            for (var i = 0; i < data.length; i++) {
                if (data[i].revenueType == "M") {
                    profit = data[i].profit || 0 + " 元";
                }
                if (data[i].revenueType == "E") {
                    ethProfit = data[i].profit || "--"
                }
            }
        }
        $(".profit").text(profit);
        $(".ethProfit").text(ethProfit);

    });
    CommonClass.BaseAction({
        sqlKey: 'com.mg.mapper.UserMapper.selectAllUser',
        paramMap: {id: userInfo.id}
    }, function (data) {
        var frozeMoney = "0 元";
        if (data && data[0]) {
            frozeMoney = data[0].frozeMoney || 0 + " 元";
        }
        $(".frozeMoney").text(frozeMoney);
    });
}

/**
 * 添加监听
 */
function initEvent() {
    $("#buyBtn").on("click", function () {
        window.parent.window.navClickAction(1, null);
    });
    $(".editRevenueType").on("click", function () {
        window.location.href = webRoot + "/pages/user/editRevenueType.jsp";
    });
    $(".balanceHandler").on("click", function () {
        window.parent.window.navClickAction(2, null);
    });
    $("#searchProfitBtn").on("click", function () {
        gridTab.setSelected(gridTab.getItemAt(1));
        tabMenuClick(1);
    });
}

function initContGrid() {
    var columns = [
        {title: '合约名称', dataIndex: 'contractName', width: '30%'},
        {title: '单价', dataIndex: 'price', width: '20%'},
        {title: '算力', dataIndex: 'computingPower', width: '20%'},
        {title: '数量', dataIndex: 'number', width: '10%'},
        {title: '状态', dataIndex: 'state', width: '10%'}
    ];
    contStore = Bui.store({
        url: urlParams.contAction,
        // autoLoad: true,
        root: 'list',               //存放数据的字段名(rows)
        totalProperty: 'total',
        pageSize: 50	// 配置分页数目
    });
    contStore.load({userId: userInfo.id});
    contGrid = Bui.grid({
        render: '#contListGrid',
        columns: columns,
        width: "100%",
        store: contStore,
        bbar: {
            // pagingBar:表明包含分页栏
            pagingBar: true
        }
    });
}

function initRevenueGrid() {
    var columns = [
        {title: '日期', dataIndex: 'revernueTime', width: '20%'},
        {title: '合约标题', dataIndex: 'contractName', width: '30%'},
        {title: '总有效算力(Th/s)', dataIndex: 'computingPower', width: '12%'},
        {
            title: '收益类型', dataIndex: 'revenueType', width: '8%', renderer: function (value) {
            if (value == "M") {
                return "现金收益"
            } else {
                return "ETH收益"
            }
        }
        },
        {title: '当日收益', dataIndex: 'profit', width: '10%'},
        {title: '状态', dataIndex: 'state', width: '10%'},
        {title: '说明', dataIndex: 'remark', width: '10%'}
    ];
    revenueStore = Bui.store({
        url: urlParams.revenueAction,
        // autoLoad: true,
        root: 'list',               //存放数据的字段名(rows)
        totalProperty: 'total',
        pageSize: 50	// 配置分页数目
    });
    revenueGrid = Bui.grid({
        render: '#revenueListGrid',
        columns: columns,
        width: "100%",
        store: revenueStore,
        bbar: {
            // pagingBar:表明包含分页栏
            pagingBar: true
        }
    });
}

function initRechargeGrid() {
    var columns = [
        {title: '充值时间', dataIndex: 'rechargeTime', width: '20%'},
        {title: '充值金额', dataIndex: 'amount', width: '20%'},
        {title: '状态', dataIndex: 'state', width: '20%'},
        {title: '说明', dataIndex: 'remark', width: '40%'}
    ];
    rechargeStore = Bui.store({
        url: urlParams.rechargeAction,
        root: 'list',               //存放数据的字段名(rows)
        totalProperty: 'total',
        pageSize: 50	// 配置分页数目
    });
    rechargeGrid = Bui.grid({
        render: '#rechargeGrid',
        columns: columns,
        width: "100%",
        store: rechargeStore,
        bbar: {
            // pagingBar:表明包含分页栏
            pagingBar: true
        }
    });
}

function initPresentGrid() {
    var columns = [
        {title: '提现时间', dataIndex: 'presentTime', width: '20%'},
        {title: '提现金额', dataIndex: 'amount', width: '20%'},
        {title: '提现银行', dataIndex: 'bankName', width: '30%'},
        {title: '状态', dataIndex: 'state', width: '10%'},
        {title: '说明', dataIndex: 'remark', width: '20%'}
    ];
    presentStore = Bui.store({
        url: urlParams.presentAction,
        root: 'list',               //存放数据的字段名(rows)
        totalProperty: 'total',
        pageSize: 50	// 配置分页数目
    });
    presentGrid = Bui.grid({
        render: '#presentGrid',
        columns: columns,
        width: "100%",
        store: presentStore,
        bbar: {
            // pagingBar:表明包含分页栏
            pagingBar: true
        }
    });
}

function tabMenuClick(tabId) {
    if (tabId == 0) {
        $("#contListGrid").show();
        $("#revenueListGrid").hide();
        $("#rechargeGrid").hide();
        $("#presentGrid").hide();
        contStore.load({userId: userInfo.id, start: 0, pageIndex: 0});
    } else if (tabId == 1) {
        $("#contListGrid").hide();
        $("#revenueListGrid").show();
        $("#rechargeGrid").hide();
        $("#presentGrid").hide();
        revenueStore.load({userId: userInfo.id, start: 0, pageIndex: 0});
    } else if (tabId == 2) {
        $("#contListGrid").hide();
        $("#revenueListGrid").hide();
        $("#rechargeGrid").show();
        $("#presentGrid").hide();
        rechargeStore.load({userId: userInfo.id, start: 0, pageIndex: 0});
    } else if (tabId == 3) {
        $("#contListGrid").hide();
        $("#revenueListGrid").hide();
        $("#rechargeGrid").hide();
        $("#presentGrid").show();
        presentStore.load({userId: userInfo.id, start: 0, pageIndex: 0});
    }
}